
// Thanks for this code snippet from http://www.tutorials.de/
var HTTP_GET_VARS = new Array();
var strGET = document.location.search.substr(1, document.location.search.length);
if (strGET != '') {
    var gArr = strGET.split('&');
    var i;
    for (i = 0; i < gArr.length; ++i) {
        var v = ''; var vArr = gArr[i].split('=');
        if (vArr.length > 1) { v = vArr[1]; }
        HTTP_GET_VARS[unescape(vArr[0])] = unescape(v);
    }
}

function GET(v) {
    if (!HTTP_GET_VARS[v]) { return 'undefined'; }
    return HTTP_GET_VARS[v];
}

function id(id) {
    return document.getElementById(id);
}

function bg() {

    return chrome.extension.getBackgroundPage();
}

function testDump(JSON_string) {
    var arr = JSON.parse(JSON_string);
    if (typeof arr === 'object' && arr && arr instanceof Array) {
        var i;
        for (i = 0; i < arr.length; i++) {
            if (typeof arr[i] === 'object') {
                var member;
                for (member in arr[i]) {
                    // todo: check if params, header are arrays of objects with members name, value
                    if (member.toString() == "params" || member.toString() == "header") {
                        if (typeof arr[i][member] === 'object' && arr[i][member] instanceof Array) {
                            var j;
                            for (j = 0; j < arr[i][member].length; j++) {
                                if (typeof arr[i][member][j] === 'object') {
                                    var member2;
                                    for (member2 in arr[i][member][j]) {
                                        if (member2.toString() != "name" && member2.toString() != "value") {
                                            console.log("member2 wrong: " + member2.toString());
                                            return false;
                                        }
                                    }
                                }
                                else {
                                    console.log("Not an object: " + JSON.stringify(arr[i][member][j]));
                                    return false;
                                }
                            }
                        }
                        else {
                            console.log("Not a header or parameter array");
                            return false;
                        }
                    }
                    else if (
                            member.toString() != "url" &&
                            member.toString() != "method" &&
                            member.toString() != "type" &&
                            member.toString() != "form_number" &&
                            member.toString() != "submission" &&
                            member.toString() != "session_warning"
                            ) {
                        console.log("Not a member of request object: " + member.toString());
                        return false;
                    }
                }
            }
            else {
                console.log("Not an object");
                return false;
            }
        }
    }
    else {
        console.log("Not an array");
        return false;
    }
    return arr;
}

window.onload = function () {
    // DOM processing goes here
    if (GET("r") == "LOAD") {
        id("load_dump_button").style.display = "block";
        id("load_dump_button").onclick = function () {

            var dump = testDump(id("dump_textarea").value);
            if (dump !== false) {
                bg().requests = dump; //JSON.parse(id("dump_textarea").value);
                id("dump_counter_font").innerHTML = "Loaded " + bg().requests.length + " captures!";
                id("dump_textarea").value = "";
            }
            else {
                alert("This does not look like a capture dump...");
            }

        };
        id("dump_counter_font").innerHTML = "Paste capture dump:";
    }
    else if (GET("r") == "ALL") {
        id("dump_textarea").value = JSON.stringify(bg().requests);
        id("dump_textarea").readOnly = true;
        id("dump_counter_font").innerHTML = "Dumped " + bg().requests.length + " captures";
    } else {
        var dump = [];
        var req = parseInt(GET("r"));
        var url = bg().requests[req].url;
        var i;
        var c = 0;

        for (i = 0; i < bg().requests.length; i++) {
            if (i <= req && url == bg().requests[i].url) { // speichere diesen und alle vorherigen requests zu dieser url
                dump.push(bg().requests[i]);
                c++;
            }
        }
        id("dump_textarea").value = JSON.stringify(dump);
        id("dump_textarea").readOnly = true;
        id("dump_counter_font").innerHTML = "Dumped " + c + " captures";
    }

}